/* Copyright (c) 2006 by M Aamir Maniar 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be 
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * */

namespace Generic.Algorithms.Sorting
{
	using System;
	using System.Collections.Generic;
    using System.Collections;
	using System.Text;
    using Generic.Utils;

	public abstract class SortStrategy<T> : ISortStrategy<T>
    {
        #region Member Declarations
        private Comparison<T> _comparision;
        #endregion

        #region public SortStrategy()
        /// <summary>
        /// Initializes a new instance of the <see cref="SortStrategy&lt;T&gt;"/> class.
        /// </summary>
        public SortStrategy()
        {
        }
        #endregion

        #region public SortStrategy(IList<T> list)
        /// <summary>
        /// Initializes a new instance of the <see cref="SortStrategy&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="list">The list.</param>
        public SortStrategy(IList<T> list)
        {
            ArgumentValidator.CheckIfNull<IList<T>>(list);
            _list = list;
        }
        #endregion

        #region public void Sort()
        /// <summary>
		/// Sorts this instance.
		/// </summary>
		public void Sort()
		{
			Sort(SortDirection.Ascending);
        }
        #endregion

        #region public virtual void Sort(SortDirection sortDirection)
        /// <summary>
        /// Sorts the list specified sort direction using applied sort strategy.
        /// </summary>
        /// <param name="sortDirection">The sort direction in which list to be sorted.</param>
        public virtual void Sort(SortDirection sortDirection)
        {
            ArgumentValidator.CheckIfNull(List);
            if (List.Count <= 1)
                return;
            ResetComparisionsCount();
        }
        #endregion

        #region public virtual void Sort(Comparison<T> comparision, SortDirection direction)
        /// <summary>
        /// Sorts the specified comparision.
        /// </summary>
        /// <param name="comparision">The comparision.</param>
        /// <param name="sortDirection">The sort direction.</param>
        public virtual void Sort(Comparison<T> comparision, SortDirection sortDirection)
        {
            //TODO: Sort using Comparision<T>
            _comparision = comparision;
            Sort(sortDirection);
        }
        #endregion

        #region protected virtual void Swap(int firstElement, int secondElement)
        /// <summary>
		/// Swaps the specified first element.
		/// </summary>
		/// <param name="firstElement">The first element.</param>
		/// <param name="secondElement">The second element.</param>
		protected virtual void Swap(int firstElement, int secondElement)
		{
			if (firstElement < 0 || secondElement < 0)
				throw new IndexOutOfRangeException("Index of either firstElement or secondElement is not proper.");
			if (firstElement == secondElement)
				return;
			T temp = _list[firstElement];
			_list[firstElement] = _list[secondElement];
			_list[secondElement] = temp;
        }
        #endregion

        #region protected virtual int CompareTo(T firstElement, T secondElement)
        protected virtual int CompareTo(T firstElement, T secondElement)
        {

            _comparisionsCount++;
            
            if (_comparision != null)
            {
                return _comparision(firstElement, secondElement);
            }

            if (Comparer != null)
            {
                return Comparer.Compare(firstElement, secondElement);
            }

            if (( firstElement is IComparable || firstElement is IComparable<T>) && Comparer == null)
            {
                return ((IComparable)firstElement).CompareTo(secondElement);
            }

            throw new NotSupportedException("Either T is not comparable or Comparer is not provided");
        }
        #endregion 

        #region public ISwapper<T> Swapper
        private ISwapper<T> _swapper;
		public ISwapper<T> Swapper
		{
			get { return _swapper; }
			set { _swapper = value; }
        }
        #endregion

        #region public IList<T> List
        private IList<T> _list;
        public IList<T> List
        {
            get { return _list; }
            set { _list = value; }
        }
        #endregion

        #region ISortStrategy<T> Members



        IComparer<T> _comparer = null;
        public IComparer<T> Comparer
        {
            get
            {
                return _comparer;
            }
            set
            {
                _comparer = value;
            }
        }

        #endregion

        #region public int ComparisionsCount
        int _comparisionsCount;
        public int ComparisionsCount
        {
            get { return _comparisionsCount; }
        }
        #endregion

        #region protected void ResetComparisionsCount()
        /// <summary>
        /// Resets the comparisions count.
        /// </summary>
        protected void ResetComparisionsCount()
        {
            _comparisionsCount = 0;
        }
        #endregion
    }
}
